#!/bin/sh
case "$1" in
  start)
    # Decrease tcp keep alive time, probing intervals and number of probes
    # because of the high instability of our customers' networks.
    # The default values are:
    #   net.ipv4.tcp_keepalive_time = 7200
    #   net.ipv4.tcp_keepalive_intvl = 75
    #   net.ipv4.tcp_keepalive_probes = 9
    sysctl net.ipv4.tcp_keepalive_time=600 #10min
    # Interval between probes.
    sysctl net.ipv4.tcp_keepalive_intvl=30 #30s
    # Number of consecutive probing failures before dropping the connection.
    sysctl net.ipv4.tcp_keepalive_probes=3
    # TODO(erdi): Disable IPv6 until it is fully supported in App.
    sysctl net.ipv6.conf.all.disable_ipv6=1
    # SpaceCast receives packets from a 60Mbps multicast UDP stream.
    # Avoid UDP packet loss by increasing socket buffer size.
    # rmem_default defines the default receive socket buffer size in bytes.
    sysctl net.core.rmem_default=16777216 #16MB
    # rmem_max defines the maximum receive socket buffer size in bytes.
    sysctl net.core.rmem_max=33554432 #32MB
    # Set the socket write buffer size large enough to hold a complete
    # blocksize from the streamer.
    # wmem_max defines the maximum send socket buffer size in bytes.
    sysctl net.core.wmem_max=1572864
    # Run the kernel flusher threads every 500ms (instead of every 5s)
    # On systems without a full cache most data from the 60Mbps stream is
    # written to disk. At 500ms the data can be written out relatively
    # early, without hogging the CPU.
    sysctl vm.dirty_writeback_centisecs=50
    # Make dirty pages eligible for writeout by the flusher threads after
    # 100ms (instead of 30s). On SpaceCast there is no benefit in delaying
    # the writeout and accumulating dirty pages which could be used by
    # other parts of the system.
    sysctl vm.dirty_expire_centisecs=10
    # Panic everytime there is a kernel oops
    sysctl -q kernel.panic_on_oops=1
    ;;
  stop)
    ;;
  restart|reload)
    $0 stop; $0 start ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac
